package pomodorotasks.testutils

import junit.framework.Assert

/**
 * Utility methods for swing JTable
 * @author Dawid Pytel
 */
class JTableUtils {
    public static int getColumnFromName(table, columnName) {
        int columnIndex = -1
        def columnNames = []
        table.columnCount.times { index ->
            String name = table.getColumnName(index)
            columnNames.add(name)
            if (name == columnName) {
                columnIndex = index
            }
        }
        assert columnIndex != -1 : "Cannot find column: $columnName. Available columns: $columnNames"
        return columnIndex
    }
    
    public static void assertValueAt(table, columnName, rowIndex, expectedValue) {
        int statusIndex = JTableUtils.getColumnFromName(table, columnName)
        def actualValue = table.getValueAt(rowIndex, statusIndex)
        Assert.assertEquals("Expected value in ${columnName}[$rowIndex] <$expectedValue> but was: <$actualValue>", 
            expectedValue, 
            actualValue)
    }
}

